<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>用户注册</title>

    <!-- 微信UI库 -->
    <link rel="stylesheet" href="libs/weui/weui.css">

    <!-- 微信JS库 -->
    <script type="text/javascript" src="libs/weui.js/weui.js"></script>
    <!-- jQuery -->
    <script type="text/javascript" src="libs/jquery/jquery-2.2.3.min.js"></script>
    <!-- 微信JS-SDK -->
    <script type="text/javascript" src="libs/js-sdk/jweixin-1.2.0.js"></script>
    <!-- 自定义公共JS -->
    <script type="text/javascript" src="js/app.js"></script>
</head>
<body>
    <div class="page">
        <div class="page__hd">

            <div style="margin: 20px 10px; color: #CCCCCC">
                为了您的账户安全，请用自己的手机号验证
            </div>

        </div>

        <div class="page__bd">
            <div class="weui-cells weui-cells_form">
                <form id="form">

                    <div class="weui-cell weui-cell_vcode">
                        <div class="weui-cell__hd">
                            <label class="weui-label">手机号</label>
                        </div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="tel" required tips="请输入手机号" placeholder="请输入手机号" name="mobile"/>
                        </div>
                        <div class="weui-cell__ft">
                            <a class="weui-vcode-btn" id="getValidCode">获取验证码</a>
                        </div>
                    </div>

                    <div class="weui-cell">
                        <div class="weui-cell__hd"><label class="weui-label">验证码</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" required tips="请输入4位验证码" maxlength="4" pattern="REG_VCODE" placeholder="验证码" name="validCode"/>
                        </div>
                    </div>

                    <div class="weui-cell">
                        <div class="weui-cell__hd">
                            <label class="weui-label">姓名</label>
                        </div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="text" required tips="请输入真实姓名" placeholder="请输入真实姓名" name="realName"/>
                        </div>
                    </div>

                </form>

            </div>
        </div>

        <label for="weuiAgree" class="weui-agree">
            <input id="accpt" type="checkbox" checked class="weui-agree__checkbox"/>
            <span class="weui-agree__text">
                阅读并同意<a href="javascript:void(0);">《相关条款》</a>
            </span>
        </label>

        <div class="weui-btn-area">
            <a class="weui-btn weui-btn_primary" href="javascript:" id="register">确定</a>
        </div>

        <div class="page__ft" style="text-align: center; padding-top: 10px;">
            <span>注册后，请关注公众号“学车”使用</span>
        </div>
    </div>

    <div id="console"></div>



    <script type="text/javascript">
        $(function() {
            $.ajax({
                type: 'POST',
                url: Commons.WEIXIN_OAUTH_H5_URL + '/wx/jssdk/config',
                dataType: 'JSON',
                success: function(result) {
                    wx.config({
                        debug: false, 					// 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                        appId: result.appid, 			// 必填，公众号的唯一标识
                        timestamp: result.timestamp, 	// 必填，生成签名的时间戳
                        nonceStr: result.noncestr, 		// 必填，生成签名的随机串
                        signature: result.signature,	// 必填，签名，见附录1
                        // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                        jsApiList: [
                        ]
                    });

                    wx.ready(function(){

                        //获取URL中的openid参数
                        var openid = Commons.getRequestParam('openid')
                        //用户自定义携带参数
                        var state = Commons.getRequestParam('state')
                        //推荐人openid
                        var refereeOpenid = Commons.getRequestParam('refereeOpenid')
                        //是否已注册
                        var isRegisterOk = Commons.getRequestParam('register');
                        //用户未注册进行了邀请朋友
                        var invitedMsg = Commons.getRequestParam('msg');
                        if(invitedMsg != undefined && invitedMsg != "") {
                            weui.alert(invitedMsg)
                        }


                        //用户非正常下进入注册页面，注册前验证是否已注册并绑定手机号（加强验证）
                        /*$.get(Commons.WEIXIN_OAUTH_H5_URL + '/wx/xczs/validRegister?openid='+openid, function(result) {
                            if(result.status) {
                                print('用户已注册过，跳转到业务页面')
                            } else {
                                print('可以注册...')
                            }
                        });*/

                        //判断用户是否已经注册，如果已经注册则跳转到平台首页
                        if(isRegisterOk === 'ok') {
                            var alert = weui.alert('你已经注册，请登录！', function () {
                                setTimeout(function () {
                                    window.location.replace(Commons.WEIXIN_OAUTH_H5_URL)
                                }, 100)
                            })
                        } else {

                            //获取验证码按钮
                            var validCodeBtn = $('#getValidCode') ;

                            var loading ;
                            //点击获取验证码按钮
                            validCodeBtn.click(function() {
                                var form = Commons.getFormData('#form');
                                var data = {'openid': openid, 'mobile': form.mobile};

                                $.ajax({
                                    type: 'post',
                                    data: data,
                                    url: Commons.WEIXIN_OAUTH_H5_URL + '/wx/xczs/getValidCode',
                                    beforeSend: function () {
                                        loading = weui.loading();
                                    },
                                    success: function (result) {
                                        loading.hide();
                                        if(result.status) {
                                            weui.toast('发送成功', 1000);
                                        } else {
                                            weui.alert(result.message);
                                        }
                                    },
                                    complete: function () {
                                    },
                                    error: function (data) {
                                    }
                                })

                            })

                            //用户提交注册信息
                            $('#register').click(function() {
                                if($('#accpt').prop('checked')) {

                                    var regexp = {
                                        regexp: {
                                            VCODE: /^.{4}$/
                                        }
                                    };

                                    weui.form.validate('#form', function (error) {
                                        if (!error) {
                                            var loading = weui.loading('提交中...');

                                            var form = Commons.getFormData('#form');
                                            var requestUrl = Commons.WEIXIN_OAUTH_H5_URL + '/wx/xczs/register'
                                            var data = {
                                                'openid': openid,
                                                'mobile': form.mobile,
                                                'validCode': form.validCode,
                                                'realName': form.realName
                                            };

                                            //识别用户是以何种方式进入注册页面，以下判断是以被邀请方式进入
                                            if(state != undefined && state != null && state === 'invite') {
                                                data['state'] = 'invite';
                                                data['refereeOpenid'] = refereeOpenid;
                                            }

                                            $.ajax({
                                                type: 'post',
                                                data: data,
                                                url: requestUrl,
                                                success: function (result) {
                                                    if(result.status) {
                                                        //weui.toast('注册成功', 1000);
                                                        //window.location.replace(Commons.WEIXIN_OAUTH_H5_URL)
                                                        window.location.replace(Commons.WEIXIN_OAUTH_H5_URL+"/h5/xczs/biz/register_ok.html")
                                                    } else {
                                                        weui.alert(result.message);
                                                    }
                                                },
                                                complete: function () {
                                                    loading.hide();
                                                },
                                                error: function (data) {
                                                    loading.hide();
                                                }
                                            })

                                        }

                                    }, regexp);

                                } else {
                                    weui.alert('请同意相关条款')
                                }

                            })
                        }

                        function print() {
                            for(var i=0; i<arguments.length; i++) {
                                $('#console').append(JSON.stringify(arguments[i])).append("<br>")
                            }
                        }
                    });

                    wx.error(function(res){
                        console.info(res);
                    });

                },
                error: function(e, ee) {
                    console.info(e);
                }
            })
        });
    </script>


</body>
</html>